{extend name="default/layout" /}

{block name="content"}
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">

    <!-- 搜索结果标题 -->
    <div class="mb-8">
        {if $keyword}
        <h1 class="text-3xl font-bold text-gray-800 mb-2">
            搜索结果: "{$keyword}"
        </h1>
        <p class="text-gray-600">共找到 {$total} 个相关商品</p>
        {else}
        <h1 class="text-3xl font-bold text-gray-800 mb-2">商品搜索</h1>
        <p class="text-gray-600">请输入关键词搜索商品</p>
        {/if}
    </div>

    <!-- 搜索框 -->
    <div class="mb-8">
        <div class="max-w-2xl">
            <div class="relative">
                <input type="text" id="search-keyword" value="{$keyword}" placeholder="请输入商品名称或关键词..."
                    class="w-full px-4 py-3 pr-12 border border-gray-300 rounded-sm focus:ring-2 focus:ring-blue-500 focus: ransparent text-lg">
                <button onclick="doSearch()" class="absolute right-3 top-3 text-gray-400 hover:text-gray-600">
                    <i class="fas fa-search text-lg"></i>
                </button>
            </div>
        </div>
    </div>
    <!-- 货币符号标识 -->
    <span id="currency-symbol" class="hidden">{:conf('site_currency_symbol')}</span>
    {if $keyword}
    <!-- 排序选项 -->
    <div class="flex items-center justify-between mb-6 pb-4 border-b border-gray-200">
        <div class="flex items-center space-x-4">
            <span class="text-sm text-gray-500">排序:</span>
            <select id="sort-select" onchange="changeSort()"
                class="px-3 py-1 border rounded focus:ring-2 focus:ring-blue-500 focus: ransparent">
                <option value="relevance">相关度</option>
                <option value="price_asc">价格从低到高</option>
                <option value="price_desc">价格从高到低</option>
                <option value="sales_desc">销量从高到低</option>
                <option value="time_desc">最新发布</option>
            </select>
        </div>

        <div class="flex items-center space-x-2">
            <button onclick="changeView('grid')" id="grid-view"
                class="p-2 rounded text-gray-500 hover:text-blue-500 active">
                <i class="fas fa-th-large"></i>
            </button>
            <button onclick="changeView('list')" id="list-view"
                class="p-2 rounded text-gray-500 hover:text-blue-500">
                <i class="fas fa-list"></i>
            </button>
        </div>
    </div>

    <!-- 搜索结果 -->
    {if $goods_list}
    <div id="goods-container">
        <!-- 桌面端网格布局 - 每行5个 -->
        <div class="hidden md:grid md:grid-cols-5 gap-6">
            {volist name="goods_list" id="goods"}
            <a href="{$goods.url}" class="block h-full group">
                <div
                    class="bg-white rounded-sm overflow-hidden hover:border-blue-500 transition-colors h-full flex flex-col">
                    <!-- 商品图片 -->
                    <div class="relative aspect-w-2 aspect-h-1 bg-gray-100 overflow-hidden">
                        <img src=""
                            data-src="{$goods.image}" alt="{$goods.name}" class="w-full h-32 object-cover lazy-image">
                    </div>

                    <!-- 商品信息 -->
                    <div class="p-3 flex-1 flex flex-col">
                        <div class="text-sm font-medium text-gray-800 line-clamp-2 h-10 leading-5 flex-1">
                            {$goods.name}
                        </div>
                        <div class="flex items-center justify-between mt-2">
                            <div class="flex items-center">
                                <span class="text-blue-500 font-bold price-container flex gap-1 items-center">
                                    <span class="text-xs currency-symbol">{:conf('site_currency_symbol')}</span>
                                    <span class="text-lg price-number">{$goods.price}</span>
                                </span>
                            </div>
                            <div class="flex items-center space-x-1">
                                <span class="text-xs text-gray-500">
                                    <i class="fas fa-eye mr-1"></i><span class="view-count"
                                        data-count="{$goods.views|default=0}">{$goods.views|default=0}</span>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
            {/volist}
        </div>

        <!-- 移动端列表布局 - 一排一个 -->
        <div class="md:hidden space-y-3">
            {volist name="goods_list" id="goods"}
            <a href="{$goods.url}" class="block group">
                <div class="bg-white rounded-sm overflow-hidden hover:border-blue-500 transition-colors">
                    <div class="flex gap-3 p-3">
                        <!-- 商品图片 -->
                        <div class="flex-shrink-0 w-20 h-20 bg-gray-100 rounded overflow-hidden">
                            <img src=""
                                data-src="{$goods.image}" alt="{$goods.name}"
                                class="w-full h-full object-cover lazy-image">
                        </div>

                        <!-- 商品信息 -->
                        <div class="flex-1 min-w-0 flex flex-col justify-between">
                            <div class="text-sm font-medium text-gray-800 mb-2 line-clamp-2 leading-tight">
                                {$goods.name}
                            </div>
                            <div class="flex items-center justify-between">
                                <div class="flex items-center">
                                    {if !empty($goods.price)}
                                    <span class="text-blue-500 font-bold price-container flex gap-1 items-center">
                                        <span class="text-xs currency-symbol">{:conf('site_currency_symbol')}</span>
                                        <span class="text-base price-number">{$goods.price}</span>
                                    </span>
                                    {else}
                                    <span class="text-green-500 font-bold text-base">免费</span>
                                    {/if}
                                </div>
                                <div class="flex items-center space-x-1">
                                    <span class="text-xs text-gray-500">
                                        <i class="fas fa-eye mr-1"></i><span class="view-count"
                                            data-count="{$goods.views|default=0}">{$goods.views|default=0}</span>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
            {/volist}
        </div>
    </div>

    <!-- 加载更多提示区域 -->
    <div id="load-more-area" class="mt-8"></div>

    {elseif $keyword}
    <!-- 无搜索结果 -->
    <div class="text-center py-20">
        <div class="text-gray-500">
            <i class="fas fa-search text-6xl mb-6"></i>
            <h3 class="text-xl font-medium mb-2">未找到相关商品</h3>
            <p class="mb-6">试试其他关键词，或者浏览我们的商品分类</p>

            <!-- 搜索建议 -->
            <div class="max-w-md mx-auto">
                <h4 class="font-medium mb-3">搜索建议:</h4>
                <ul class="text-sm space-y-1">
                    <li>• 检查关键词拼写是否正确</li>
                    <li>• 尝试使用更通用的关键词</li>
                    <li>• 减少关键词数量</li>
                </ul>
            </div>

            <!-- 热门搜索 -->
            <div class="mt-8">
                <h4 class="font-medium mb-3">热门搜索:</h4>
                <div class="flex flex-wrap justify-center gap-2">
                    <a href="/search-软件"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">软件</a>
                    <a href="/search-工具"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">工具</a>
                    <a href="/search-教程"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">教程</a>
                    <a href="/search-资源"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">资源</a>
                </div>
            </div>
        </div>
    </div>
    {/if}

    {else}
    <!-- 搜索提示 -->
    <div class="text-center py-20">
        <div class="text-gray-500">
            <i class="fas fa-search text-6xl mb-6"></i>
            <h3 class="text-xl font-medium mb-2">开始搜索</h3>
            <p class="mb-6">在上方搜索框中输入关键词，找到您需要的商品</p>

            <!-- 热门搜索 -->
            <div class="mt-8">
                <h4 class="font-medium mb-3">热门搜索:</h4>
                <div class="flex flex-wrap justify-center gap-2">
                    <a href="/search-软件"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">软件</a>
                    <a href="/search-工具"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">工具</a>
                    <a href="/search-教程"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">教程</a>
                    <a href="/search-资源"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">资源</a>
                    <a href="/search-模板"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">模板</a>
                    <a href="/search-插件"
                        class="px-3 py-1 bg-gray-100 rounded-full text-sm hover:bg-gray-200 transition-colors">插件</a>
                </div>
            </div>
        </div>
    </div>
    {/if}

</div>
{/block}

{block name="js"}
<!-- 引入外部JS文件 -->
<script src="/static/default/js/goods/search.js?v={$static_version}"></script>

<!-- 页面初始化 -->
<script>
    $(document).ready(function () {
        // 初始化搜索页面
        if (typeof SearchPage !== 'undefined') {
            const keyword = '{$keyword}';
            const totalPages = Math.ceil({$total|default=0} / {$per_page|default=20});
    SearchPage.init(keyword, totalPages);
    }
});

</script>
{/block}